package cn.com.gridinfo.sooc.core.my.note.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.my.note.bean.MyNoteDetail;
import cn.com.gridinfo.sooc.utils.DateUtil;

/**
 * 笔记详情adapter
 * Created by luxiaochen on 2015/12/22.
 */
public class NoteDetailListAdapter extends UltimateViewAdapter<NoteDetailListAdapter.ViewHolder> {

    public List<MyNoteDetail> myNoteDetailList;
    private ViewHolder viewHolder;
    Context context;

    public NoteDetailListAdapter(List<MyNoteDetail> list, Context context) {
        this.myNoteDetailList = list;
        this.context = context;

    }


    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_note_detail_listview, parent, false);
        return new ViewHolder(view, true);
    }

    /**
     * 点击事件接口
     */
    public interface OnItemLongClickListener {
        void OnItemClick(View view, MyNoteDetail myNoteDetail, int position);

    }

    private OnItemLongClickListener onItemLongClickLitener;

    public void setOnItemLongClickLitener(OnItemLongClickListener onItemLongClickLitener) {
        this.onItemLongClickLitener = onItemLongClickLitener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= myNoteDetailList.size() : position < myNoteDetailList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            MyNoteDetail myNoteDetail = myNoteDetailList.get(position);
            holder.bjnr.setText(myNoteDetail.getBjnr());//笔记内容
            holder.mlmc.setText("来自\" " + myNoteDetail.getMlmc() + " \"");//目录名称
            if (myNoteDetail.getSfgk().equals("0")) {
                // 是否公开 0：不 1：公开
                holder.zan.setVisibility(View.GONE);//不公开，隐藏赞的数量
                holder.sfgk.setVisibility(View.VISIBLE);//不公开 显示标志
            } else {
                holder.zan.setVisibility(View.VISIBLE);//公开，显示赞的个数
                holder.sfgk.setVisibility(View.GONE);//公开，隐藏标志
            }
            holder.zannum.setText(myNoteDetail.getZanCount());//点赞数量
            holder.inputTime.setText(myNoteDetail.getTime_show());
            holder.rl.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemLongClickLitener.OnItemClick(v, myNoteDetailList.get(position), position);
                    return true;
                }
            });


        }
    }

    //加载更多
    public void loadmore(List<MyNoteDetail> list) {
        myNoteDetailList.addAll(list);
        notifyDataSetChanged();
    }

    // 刷新
    public void refresh(List<MyNoteDetail> list) {
        clear(myNoteDetailList);
        myNoteDetailList.addAll(list);
        notifyDataSetChanged();
    }

    //删除
    public void delete(int position) {
        myNoteDetailList.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 获取我的笔记的条数
     */
    public int getListCount() {
        return myNoteDetailList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getAdapterItemCount() {
        return myNoteDetailList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView bjnr, mlmc, zannum, inputTime;
        ImageView sfgk;
        LinearLayout zan;
        RelativeLayout rl;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
//                //控件布局
                bjnr = (TextView) itemView.findViewById(R.id.bjnr);//笔记内容
                mlmc = (TextView) itemView.findViewById(R.id.mlmc);//目录名称
                sfgk = (ImageView) itemView.findViewById(R.id.sfgk);//是否公开
                zannum = (TextView) itemView.findViewById(R.id.zannum);//点赞数量
                inputTime = (TextView) itemView.findViewById(R.id.input_time);//加入时间
                zan = (LinearLayout) itemView.findViewById(R.id.zan_lr);//赞的rl 是否隐藏
                rl = (RelativeLayout) itemView.findViewById(R.id.rl);
            }
        }
    }
}
